package thread;

import java.util.concurrent.locks.LockSupport;

/**
 * 如果一个线程在不可中断的状态触发中断，然后进行wait，会怎么样？
 */
public class ThreadInterruptDemo2 {
    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread(() -> {
            System.out.println("thread running");
            double d = 0;
            for (int i = 0; i < 50_000_000; i++) {
                d += (d + Math.E) / Math.PI;
            }
            System.out.println("calc completed");
            System.out.println("thread.isInterrupted -> " + Thread.currentThread().isInterrupted());
//            try {
//                Thread.sleep(1000);
//            } catch (InterruptedException e) {
//                System.out.println("sleep interrupted");
//            }
//            try {
//                synchronized (ThreadInterruptDemo2.class) {
//                    ThreadInterruptDemo2.class.wait();
//                }
//            } catch (InterruptedException e) {
//                System.out.println("wait interrupted");
//            }
            try {
                LockSupport.park();
                System.out.println("park auto wake up");
                LockSupport.parkNanos(Long.MAX_VALUE);
                System.out.println("park auto wake up twice");
            } catch(Exception e) {
                System.out.println("park error");
            }
        });
        thread.start();
        Thread.sleep(100);
        System.out.println("interrupt the thread");
        thread.interrupt();
    }
}
